package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.payconfig.EntityXfTimeZone;
import com.ocom.common.entity.payconfig.EntityXfZoneDate;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.RemoveConsDateZoneRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.webapi.DateListVo;
import com.ocom.common.vo.webapi.WebDayListVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/4/18
 **/
@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class ConsTimeZoneController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    /**
     * 3.17.1消费时区管理列表（ConsTimeZone/onShow）
     */
    @PostMapping(WebApiServiceUrl.Url_ConsTimeZoneOnShow)
    public Result ConsTimeZoneOnShow() {
        Map<String, Object> resMap = new HashMap<>();

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        Result result1 = payConfigClientService.getConsZoneList(yoCiUser.getComId());
        Result result2 = payConfigClientService.getConsZoneDateList(yoCiUser.getComId(), 1, 500);


        Map<Integer, String> zoneNameMap = new HashMap<>();
        List<EntityXfTimeZone> entityXfTimeZones = new ArrayList<>();
        if (result1.getCode() == 200) {
            entityXfTimeZones = JSON.parseArray(JSON.toJSONString(result1.getData()), EntityXfTimeZone.class);
            //list转map
            zoneNameMap = entityXfTimeZones.stream().collect(Collectors.toMap(e -> e.getZoneNo(),
                    e -> e.getZoneName(),
                    (e1, e2) -> e1));
        }
        resMap.put("zoneList", entityXfTimeZones);

        /**
         * dayList	周期列表	复合域	固定的七行数据，0-6:周日-周六	是
         * dateList	不固定日期列表	复合域		是
         * zoneList	时区列表	复合域	时区编号和时区名称	是
         */
        List<EntityXfZoneDate> entityXfZoneDateList = new ArrayList<>();
        if (result2.getCode() == 200) {
            JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(result2.getData()));
            entityXfZoneDateList = JSON.parseArray(JSON.toJSONString(jsonObject1.get("list")), EntityXfZoneDate.class);
        }


        List<WebDayListVo> dayListVos = new ArrayList<>();
        for (Integer i = 0; i <= 6; i++) {
            String datI = i.toString();
            WebDayListVo webDayListVo = new WebDayListVo();
            webDayListVo.setDayDate(0);
            webDayListVo.setDate(datI);  //0-6
            webDayListVo.setZoneNo(0);
            webDayListVo.setZoneName("");
            for (EntityXfZoneDate entityXfZoneDate : entityXfZoneDateList) {
                if (entityXfZoneDate.getDayDate() == 0 && entityXfZoneDate.getDate().equals(datI)) {
                    if (zoneNameMap.containsKey(entityXfZoneDate.getZoneNo())) {
                        webDayListVo.setZoneNo(entityXfZoneDate.getZoneNo());
                        webDayListVo.setZoneName(zoneNameMap.get(entityXfZoneDate.getZoneNo()));
                    }
                    break;
                }
            }
            dayListVos.add(webDayListVo);
        }
        resMap.put("dayList", dayListVos);

        List<DateListVo> dateListVos = new ArrayList<>();
        for (EntityXfZoneDate entityXfZoneDate : entityXfZoneDateList) {
            if (entityXfZoneDate.getDayDate() == 1) {
                DateListVo dateListVo = new DateListVo();
                dateListVo.setDayDate(1);
                dateListVo.setDate(entityXfZoneDate.getDate());
                dateListVo.setZoneNo(0);
                dateListVo.setZoneName("");
                if (zoneNameMap.containsKey(entityXfZoneDate.getZoneNo())) {
                    dateListVo.setZoneNo(entityXfZoneDate.getZoneNo());
                    dateListVo.setZoneName(zoneNameMap.get(entityXfZoneDate.getZoneNo()));
                }
                dateListVos.add(dateListVo);
            }
        }

        resMap.put("dateList", dateListVos);


        return ResultUtil.success(resMap);
    }


    /**
     * 3.17.2消费时区权限设置（ConsTimeZone/modify）
     */
    @PostMapping(WebApiServiceUrl.Url_ConsTimeZoneModify)
    public Result ConsTimeZoneModify(@RequestBody EntityXfZoneDate entityXfZoneDate) {
        return payConfigClientService.setConsDateZone(entityXfZoneDate);
    }


    /**
     * 3.17.3消费时区删除（ConsTimeZone/delete）
     */
    @PostMapping(WebApiServiceUrl.Url_ConsTimeZoneDelete)
    public Result ConsTimeZoneDelete(@RequestBody RemoveConsDateZoneRequest request) {
        if (request.getDayDate() != 1) {
            return ResultUtil.error(ResultEnum.Err_3016);
        }
        return payConfigClientService.removeConsDateZone(request.getComId(), request.getDate());
    }

    /**
     * 3.17.4增加消费时区（ConsTimeZone/new）
     */
    @PostMapping(WebApiServiceUrl.Url_ConsTimeZoneNew)
    public Result ConsTimeZoneNew(@RequestBody EntityXfTimeZone entityXfTimeZone) {
        return payConfigClientService.addConsZone(entityXfTimeZone);
    }

    /**
     * 3.17.5修改消费时区（ConsTimeZone/modifyName）
     */
    @PostMapping(WebApiServiceUrl.Url_ConsTimeZoneModifyName)
    public Result ConsTimeZoneModifyName(@RequestBody EntityXfTimeZone entityXfTimeZone) {
        return payConfigClientService.updateConsZoneName(entityXfTimeZone);
    }
}
